#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "QDebug"
#include "CircuitGraph.h"
#include "Gate.h"
#include "ANDGate.h"
#include "ORGate.h"
#include "QMouseEvent"
MainWindow::MainWindow(QWidget *parent)//
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //采用邻接表存储电路图，使用vector动态添加门
    circuit= new CircuitGraph();
    //input工具栏连接lambda表达式槽函数
    connect(ui->actionInput,&QAction::triggered,this,[=]()mutable{
        Input in=Input();
        inputs.push_back(in);
    });
    //output工具栏连接lambda表达式槽函数
    //与门按钮连接lambda表达式槽函数
    connect(ui->actionOutput,&QAction::triggered,this,[=]()mutable{
        Output out=Output();
        outputs.push_back(out);
    });
    connect(ui->actionAND,&QAction::triggered,this,[=]()mutable{
    VNode tempnode=VNode();
    Gate tempgate=ANDGate(50,50);
    tempnode.gate=tempgate;
    tempnode.first=NULL;
    circuit->gates.push_back(tempnode);
    qDebug()<<"与门创建成功";
    });
    //鼠标点击事件与selectGate函数相连
    //connect(this,)
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    drawGate(&painter,circuit->gates);
    Draw_Input_Output(&painter);
}

void MainWindow::drawGate(QPainter *painter, QVector <VNode> gates)//VNode* gate
{
    for(int i=circuit->gates.length()-1;i>=0;i--)//逆序画门
    {
        if(circuit->gates[i].gate.clickState==2)
            painter->setPen(Qt::red);
        else
            painter->setPen(Qt::blue);
        painter->drawRect(circuit->gates[i].gate.gate_x,circuit->gates[i].gate.gate_y,50,50);
        this->update();//刷新
    }
}

void MainWindow::Draw_Input_Output(QPainter *painter)
{
    for(int i=this->inputs.length()-1;i>=0;i--)
    {
        if(this->inputs[i].clickState==2)
            painter->setPen(Qt::red);
        else
            painter->setPen(Qt::blue);
        painter->drawRect(this->inputs[i].input_x,this->inputs[i].input_y,20,20);
        this->update();//刷新
    }
}
void MainWindow::select_Input_Output_1(int mouse_x,int mouse_y)
{
    int i;
    //选择输入
     for(i=0;i<this->inputs.length();i++)
     {
         if(mouse_x > this->inputs[i].input_x && mouse_x <this->inputs[i].input_x+20 &&mouse_y > this->inputs[i].input_y && mouse_y <this->inputs[i].input_y+20)
         {
            // this->gates[i].gate.isSelected=true;
             if(this->inputs[i].clickState==0 )
                 this->inputs[i].clickState=1;
             qDebug()<<"鼠标按下了！！";
             qDebug()<<this->inputs[i].clickState;
             break;
         }
     }
//     //选择输出
//     for(i=0;i<this->outputs.length();i++)
//     {
//         if(mouse_x > this->outputs[i]. && mouse_x <this->inputs[i].input_x+20 &&mouse_y > this->inputs[i].input_y && mouse_y <this->inputs[i].input_y+20)
//         {
//             // this->gates[i].gate.isSelected=true;
//             if(this->inputs[i].clickState==0 )
//                 this->inputs[i].clickState=1;
//             qDebug()<<"鼠标按下了！！";
//             qDebug()<<this->inputs[i].clickState;
//             break;
//         }
//     }


}
void MainWindow::select_Input_Output_2(int mouse_x,int mouse_y)
{
    int i;
    bool clearFlag=true;
    for(i=0;i<this->inputs.length();i++)
    {
        if(mouse_x > this->inputs[i].input_x && mouse_x <this->inputs[i].input_x+20 &&mouse_y > this->inputs[i].input_y && mouse_y <this->inputs[i].input_y+20)
        {
            clearFlag=false;//
            if(this->inputs[i].clickState==1)
            this->inputs[i].clickState=2;
            qDebug()<<"鼠标松开了！！";
            qDebug()<<this->inputs[i].clickState;
            break;
        }
       // else if(this->gates[i].gate.clickState!=2)
    }
    if(clearFlag)
    {
        for(int i=0;i<this->inputs.length();i++)
             this->inputs[i].clickState=0;
    }
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    QPoint point = e->pos();
    circuit->selectGate_1(point.x(),point.y());
    this->select_Input_Output_1(point.x(),point.y());
//    qDebug()<<point.x();
//    qDebug()<<point.y();
}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
     QPoint point = e->pos();
     //监测门的移动
     for(int i=0;i<circuit->gates.length();i++)
     {
         if(circuit->gates[i].gate.clickState==2)
         {
                         circuit->gateMove(point.x(),point.y(),i);
                         qDebug()<<"移动了！";
                         QWidget::repaint();
                                  break;
         }
     }
     //监测input的移动
      for(int i=0;i<this->inputs.length();i++)
      {
          if(this->inputs[i].clickState==2)
          {
              this->inputs[i].input_x+=(point.x()-this->inputs[i].input_x);
              this->inputs[i].input_y+=(point.y()-this->inputs[i].input_y);
                          qDebug()<<"移动了！";
                          QWidget::repaint();
                                   break;
          }
      }

}

void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
   QPoint point = e->pos();
   circuit->selectGate_2(point.x(),point.y());
   this->select_Input_Output_2(point.x(),point.y());
   ConnectGateAndGate(circuit);
}

//待测试代码
void MainWindow::ConnectGateAndGate(CircuitGraph *circuit)
{
    int from,to;
    bool flag_from=false,flag_to=false;
    for(int i=circuit->gates.length()-1;i>=0;i--)//逆序画门
    {
        if(circuit->gates[i].gate.clickState==1)
        {
           from=i;
           flag_from=true;
           break;
        }
    }
    for(int i=circuit->gates.length()-1;i>=0;i--)//逆序画门
    {
        if(circuit->gates[i].gate.clickState==3)
        {
            to=i;
            flag_to=true;
            break;
        }
    }
    if(flag_from&&flag_to)
        circuit->setEdge(from,to);
}
MainWindow::~MainWindow()
{
    delete ui;
}


